package com.ruoyi.web.controller.interview;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.interview.domain.InterviewDimension;
import com.ruoyi.interview.domain.InterviewPosition;
import com.ruoyi.interview.domain.InterviewQuestion;
import com.ruoyi.interview.service.*;
import com.ruoyi.web.util.ChatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/interview/question")
public class InterviewQuestionController {

    @Autowired
    private IInterviewPositionService interviewPositionService;

    @Autowired
    private IInterviewDimensionService interviewDimensionService;

    @Autowired
    private IInterviewRecordService interviewRecordService;

    @Autowired
    private IInterviewQuestionService interviewQuestionService;
    private static final int QUESTION_NUMBER=5;

    /**
     * 生成模拟面试问题
     * @param positionId
     * @return
     */

    @GetMapping("/genQuestion")
    public AjaxResult genQuestion(Long positionId ){
        //1.根据前端传回来的岗位ID查询岗位信息
        InterviewPosition interviewPosition =
                interviewPositionService.selectInterviewPositionById(positionId);
        //2.根据岗位的名称生成模拟面试的问题
        //查询对应的维度信息，根据岗位ID去查询的
        List<InterviewDimension> dimensionList = interviewDimensionService.selectDimensionByPostionId(positionId);
        //创建一个方法，拼接岗位维度和岗位信息的字符串
        String dimensions = genDimensionPrompt(dimensionList);
        IQuestionGenerateService service =
                ChatUtil.getService(IQuestionGenerateService.class);
        String message = service.generateQuestion(interviewPosition.getName(), QUESTION_NUMBER,dimensions);
        //3.生成一次面试记录 解析此次记录的问题，保存道数据库中
         Long recordId = interviewRecordService.gennerateRecord(positionId,message);

        return AjaxResult.success(recordId);
    }

    private String genDimensionPrompt(List<InterviewDimension> dimensionList) {
        return dimensionList.stream().map(dimension ->{
            return "[" + dimension.getName() + "(" +dimension.getWeight() +")]";
        }).collect(Collectors.joining(","));
    }

    /**
     * 根据面试记录查询问题列表
     * @param recordId
     * @return
     */
    @GetMapping("/getQuestions")
    public AjaxResult getQuestions(Long recordId){
        InterviewQuestion queryVo = new InterviewQuestion();
        queryVo.setRecordId(recordId);
        List<InterviewQuestion> interviewQuestions = interviewQuestionService.selectInterviewQuestionList(queryVo);
        return AjaxResult.success(interviewQuestions);
    }
}
